import { ui } from "../../ui/layaMaxUI";
import Game from "../../core_wq/game/Game";
import GameEvent from "../../core_wq/game/GameEvent";
import GameStorage from "../../core_wq/game/GameStorage";
import SoundMgr from "../../core_wq/sound/SoundMgr";
import SoundType from "../../core_wq/sound/SoundType";
import EffectUtil from "../../core_wq/utils/EffectUtil";
import { GameStageType } from "../../core_wq/game/GameStageType";



export default class GameOverView extends ui.moduleView.gameOver.GameOverViewUI {


    _isLock = false;

    onEnable() {
        // console.log("GameOverView show..")
        if (this.advBtn) {
            this.scale(0, 0);
            this.receiveBtn.on(Laya.Event.CLICK, this, this.onClickReceiveBtn);
            this.advBtn.on(Laya.Event.CLICK, this, this.onClickAdvBtn);

            Laya.Tween.to(this, { scaleX: 1, scaleY: 1 }, 700, Laya.Ease.quadOut);
            Laya.timer.clearAll(this);
            this.receiveBtn.visible = false;
            Laya.timer.once(4000, this, () => {
                this.receiveBtn.visible = true;
            })
            this.onWin();
        }
    }

    onClickReceiveBtn() {
        if (this._isLock === true) {
            return;
        }
        this._isLock=true;
        this.playEffect();
    }
    onClickAdvBtn() {
        if (this._isLock === true) {
            return;
        }
        this.playEffect();
        this._isLock=true;
        Game.hero.playerData.gold += Game.gameStage.stageVO.bonusGold * 2;
    }

    playEffect() {
        if(Game.gameStage.stageVO.stageType===GameStageType.BOSS){
            EffectUtil.playDiamondFlyEffect(this.box.localToGlobal(new Laya.Point(this.receiveBtn.x, this.receiveBtn.y)), this, () => {
                Game.hero.playerData.diamond += Game.gameStage.stageVO.bonusDiamond;
                // console.log("playCoinFlyEffect", Game.hero.playerData.gold)
                this.removeSelf();
                GameStorage.saveStorage();
                Laya.stage.event(GameEvent.GO_HOME);
            });

        }
        else{
            EffectUtil.playCoinFlyEffect(this.box.localToGlobal(new Laya.Point(this.receiveBtn.x, this.receiveBtn.y)), this, () => {
                Game.hero.playerData.gold += Game.gameStage.stageVO.bonusGold;
                // console.log("playCoinFlyEffect", Game.hero.playerData.gold)
                this.removeSelf();
                GameStorage.saveStorage();
                Laya.stage.event(GameEvent.GO_HOME);
            });
        }
     
    }
    onWin() {
        SoundMgr.Ins.stopBg();
        SoundMgr.Ins.playEffect(SoundType.WIN);
        if(Game.gameStage.stageVO.stageType===GameStageType.BOSS){
            this.bonusImg.skin="images/gameOver/diamonds.png";
        }
        else{
            this.bonusImg.skin="images/gameOver/golds.png";
        }
        Game.hero.playerData.mapLevel++;
        let n = Game.gameStage.stageData.curHp / Game.gameStage.stageData.maxHp;

        // console.log(Game.gameStage.stageData.curHp,Game.gameStage.stageData.maxHp)

        this.showStar(this.star1, 0);

        if (n > 0.34) {
            this.showStar(this.star2, 500);
        }
        if (n >= 1) {
            this.showStar(this.star3, 1000);
        }

        GameStorage.saveStorage();
      
    }

    showStar(star: Laya.Sprite, delay: number) {
        star.loadImage("images/gameOver/star.png")
        star.scale(0, 0);
        Laya.Tween.to(star, { scaleX: 1, scaleY: 1 }, 1000 + delay, Laya.Ease.quadOut);
    }


}